<!--
 * @Description: 农资分类管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:zhactbar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save"
      />
    </template>
    <template v-slot:tablebox>
      <el-tree
        ref="tree"
        :props="treerule"
        :data="treeData"
        show-checkbox
        node-key="inputTypeId"
        default-expand-all
        @node-click="handleEdit"
        @check-change="handleSelectionChange"
      />
    </template>
    <template v-slot:form>
      <el-form
        ref="formData"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-form-item label="类型名称:" prop="varietyName" class="w100p">
          <el-input
            v-model="formData.typeName"
            :disabled="!canInput"
            clearable
          />
        </el-form-item>
        <el-form-item label="备注:" prop="specification" class="w100p">
          <el-input
            v-model="formData.remark"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            :disabled="!canInput"
            clearable
          />
        </el-form-item>
      </el-form>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
// import basicInfoManage from "@/api/basic-info-manage/agr-source-Type-Manage";
import BaseMainSlot from "@/components/BaseMainSlot";
import { mymixin } from "@/utils/mixin";
export default {
  name: "",
  components: { ActionBar, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      treerule: {
        children: "child",
        label: "typeName"
      },
      rules: {},
      treeData: []
    };
  },
  watch: {},
  created() {
    // 获取树状图
    this.getTreeDataList();
  },
  mounted() {},
  methods: {
    getTreeDataList() {
      this.loading = true;
      this.formData = {};
      // basicInfoManage.getTreeList().then(res => {
      //   this.loading = false;
      //   let data = res.data;
      //   // 赋值数组
      //   this.treeData = data;
      //   console.log(this.treeData);
      //   this.resetActionBar();
      // });
    },
    handleEdit(row) {
      // TODO 点击行元素后 edit
      this.formData = row;
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      this.canInput = false;
    },
    handleSelectionChange() {
      let list = this.$refs.tree.getCheckedKeys();
      this.inputTypeIdList = [];
      this.inputTypeIdList = list;
      if (this.inputTypeIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      // basicInfoManage
      //   .allmetInfo({ inputTypeIdList: this.inputTypeIdList }, "delList")
      //   .then(res => {
      //     if (res.code === 1) {
      //       this.$message.success(res.msg);
      //       this.getTreeDataList();
      //     }
      //   });
    },
    save() {
      if (this.nowChanceStau === "edit") {
        // 编辑接口
        // let nowdataone = {
        //   remark: this.formData.remark,
        //   parentId: this.formData.parentId,
        //   typeName: this.formData.typeName,
        //   inputTypeId: this.formData.inputTypeId
        // };
        // // 新增点的对象数据数组
        // let arr = this.treeData.filter(
        //   item => item.inputTypeId === this.inputTypeId
        // );
        // let idList = this.treeData.map(item => item.inputTypeId);
        // if (idList.includes(arr[0].inputTypeId)) {
        //   this.$message.error("该巡查点已经存在~");
        // } else {
        // basicInfoManage.allmetInfo(nowdataone, "edit").then(res => {
        //   if (res.code === 1) {
        //     this.$message.success("修改成功！");
        //     this.getTreeDataList();
        //   } else {
        //     this.$message.error("修改失败！");
        //   }
        // });
        // }
      } else if (this.nowChanceStau === "add") {
        let nowdata = {
          remark: this.formData.remark,
          typeName: this.formData.typeName
        };
        if (this.formData.inputTypeId) {
          nowdata.parentId = this.formData.inputTypeId;
        } else {
          nowdata.parentId = 0;
        }
        // 新增接口
        // basicInfoManage.allmetInfo(nowdata, "add").then(res => {
        //   if (res.code === 1) {
        //     this.$message.success("新增成功！");
        //     this.getTreeDataList();
        //   } else {
        //     this.$message.error("新增失败！");
        //   }
        // });
      } else {
        this.$message.fail("选择失败！");
      }
    }
  }
};
</script>

<style scoped lang="scss"></style>
